package com.tfswx.jkgl.core.config;

import com.alibaba.fastjson.JSON;
import com.tfswx.jkgl.core.bean.JkglRuntimeData;
import com.tfswx.jkgl.core.consts.Const;
import com.tfswx.jkgl.core.utils.sm4.DecryptSm4InputStream;
import org.apache.commons.io.IOUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.FileInputStream;
import java.nio.charset.StandardCharsets;

/**
 * @author ricky
 * @date 2021/12/15
 */
@Component
public class JkglDataConfig {

    @Bean
    JkglRuntimeData jkglRuntimeData() {
        return loadJkglData();
    }

    public JkglRuntimeData loadJkglData() {
        try {
            JkglRuntimeData jkglRuntimeData = new JkglRuntimeData();
            System.out.println("jar 中");
            File file = new File(Const.jkConfonfigPath);
            if (file.exists()) {
                try (DecryptSm4InputStream decryptSm4InputStream = new DecryptSm4InputStream(new FileInputStream(file))) {
                    byte[] destArray = IOUtils.toByteArray(decryptSm4InputStream);
                    String s = new String(destArray, StandardCharsets.UTF_8);
                    jkglRuntimeData = JSON.parseObject(s, JkglRuntimeData.class);
                }
            }
            return jkglRuntimeData;
        } catch (Exception e) {
            throw new RuntimeException("加载接口数据失败", e);
        }
    }
}
